//
//  FavWorkCollectionView.m
//  WULIHH
//
//  Created by chen tang on 16/4/27.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "FavWorkCollectionView.h"
#import "FavWorkCollectionViewCell.h"
#import "MJRefresh.h"

#import "Header.h"
#import "WuLiUserWorkReauest.h"
#import "UserInfoModel.h"

@interface FavWorkCollectionView ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
{
    UICollectionView *mainCollectionView;
    NSMutableArray *dataMUtableArray;
    NSString *pageString;
}

@end


@implementation FavWorkCollectionView



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUserInteractionEnabled:YES];
        pageString = @"1";
        dataMUtableArray = [NSMutableArray new];
        
        UICollectionViewFlowLayout *falowout = [[UICollectionViewFlowLayout alloc] init];
        
        mainCollectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:falowout];
        [mainCollectionView setDataSource:self];
        [mainCollectionView setDelegate:self];
        
        [mainCollectionView setBackgroundColor:uicolor(236, 236, 239)];
        
        
        
        [self addSubview:mainCollectionView];
        
        [mainCollectionView registerClass:[FavWorkCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        
        mainCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadMyFavWorkList)];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userCollecWorkListGetBack:) name:yonghushoucangList object:nil];
    }
    return self;
}




- (void)loadMyFavWorkList
{
    pageString = @"1";
    [WuLiUserWorkReauest requestUserWorkCollect:[UserInfoModel userIDString] withPage:pageString];
}



//刷新数据
- (void)beginRefresh
{
    [mainCollectionView.mj_header beginRefreshing];
}


//加载数据
- (void)reloadMoreFavData
{
    
}



#pragma mnark----
- (void)userCollecWorkListGetBack:(NSNotification *)notion
{
    [mainCollectionView.mj_header endRefreshing];
    [mainCollectionView.mj_footer endRefreshing];
    
    NSDictionary *dic = [notion userInfo];
//    NSLog(@"%@", dic);
    if ([[dic valueForKey:@"success"] intValue] == 1)
    {
        if ([pageString isEqualToString:@"1"])
        {
            if ([[dic valueForKey:@"data"] count] == 0)
            {
                [dataMUtableArray removeAllObjects];
            }
            else
            {
                [dataMUtableArray removeAllObjects];
                [dataMUtableArray addObjectsFromArray:[dic valueForKey:@"data"]];
            }
        }
        else
        {
            if ([[dic valueForKey:@"data"] count] == 0)
            {
                pageString = [NSString stringWithFormat:@"%d", [pageString intValue] - 1];
            }
            [dataMUtableArray addObjectsFromArray:[dic valueForKey:@"data"]];
            
        }
        
        [mainCollectionView reloadData];

    }
    else
    {
        
    }
}




#pragma mark----datasourcec

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return dataMUtableArray.count;
}



-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"cell";
    FavWorkCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
//    [cell setTag:indexPath.row];
    [cell loadModel:dataMUtableArray[indexPath.row]];
    [cell setTag:indexPath.row];
//    [cell setBackgroundColor:[UIColor redColor]];
    return cell;
}




-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(4, 5, 4, 5);
}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(biliwith(147.0), biliwith(254.0));
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"%@", dataMUtableArray[indexPath.row]);
    [_MFavWorkCollectionViewDelegate pushWithWork:[NSString stringWithFormat:@"%d", (int)indexPath.row] withInfo:dataMUtableArray[indexPath.row]];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
